/**
 * Created by Administrator on 2015/2/8 0008.
 */
var userApp = angular.module("app.userIndex",[]);
var userId;
function delArray(obj,id){
    var copyObj = obj;
    for(var i =0 ;i<copyObj.length;i++){
        if(copyObj[i].id == id){
            obj.splice(i,1);
        }
    }
}
//用户主界面
userApp.controller("userIndex",['$scope','$http','ngDialog','$filter',function($scope,$http,ngDialog,$filter){
    $http.get('management/user/findByPage?pageNum=1').
        success(function(data) {
            $scope.user =  data.list;
            $scope.total = data.total;
            $scope.pageSize = data.pageSize;
            $scope.numPerPage = 1;
            $scope.search = "";
            $scope.user.length == 0 ? $scope.userAddHide = false : $scope.userAddHide = true;
        });
    $scope.keyupQuery = function(){
        var name = $scope.search;
        var reg = /^[\u4e00-\u9fa5]*$/;
        if(reg.test(name)&&name.length<6){
            $scope.pageChange(1);
        }
    };
    $scope.order = function(rowName) {
        return $scope.row !== rowName ? ($scope.row = rowName,
            $scope.user = $filter("orderBy")($scope.user, rowName), $scope.onOrderChange()) : void 0
    };
    $scope.pageChange = function(page){
        $http.get('management/user/findByPage',{params: {pageNum:page,name:$scope.search}}).
            success(function(data) {
                $scope.user =  data.list;
                $scope.total = data.total;
                $scope.pageSize = data.pageSize;
                $scope.user.length == 0 ? $scope.userAddHide = false : $scope.userAddHide = true;
            });
    };
    $scope.delUser = function(id){
        $http.get('management/user/del?id='+id).success(function(data){
            delArray($scope.user,id);
        });
    };
    $scope.openUserInfo = function(id){
        userId = id;
        ngDialog.open({
            template: 'userIndex/userInfo',
            className:'ngdialog-theme-flat',
            width:950,
            height:600
        });
    };
    $scope.openUpdateUser = function(id){
        userId = id;
    }
}]);

//用户详细信息
userApp.controller("userInfo",['$scope','$http',function($scope,$http){
    $http.get('management/user/findInfo?id='+userId).success(function(data){
        $scope.info = data;
        var job = $scope.info.jobstate;
        if(job == 1)$scope.info.jobstate='试用';
        else if(job == 2)$scope.info.jobstate='转正';
        else $scope.info.jobstate='离职';
        var relation = $scope.info.relation;
        if(relation == 1)$scope.info.relation='父子';
        else if(relation == 2)$scope.info.relation='父女';
        else if(relation == 3)$scope.info.relation='母子';
        if(relation == 4)$scope.info.relation='母女';
    });
}]);

//修改用户信息
userApp.controller("updateUser",['$scope','$http','$location','$timeout',function($scope,$http,$location,$timeout){
    $http.get('management/user/findById?id='+userId).success(function(data){
        $scope.info = data;
    });
    $http.get('management/zone/findByStateY').success(function(data){
        $scope.zone = data;
    });
    $http.get('management/dept/findByStateY').success(function(data){
        $scope.dept = data;
    });
    $http.get('management/role/findItem').success(function(data){
        $scope.role = data;
    });
    $scope.updateUser = function(){
        if($("#dimissiondate").val().length > 0)
        $scope.info.dimissiondate = $("#dimissiondate").val();
        $scope.isHide = false;
        $http({
            method  : 'post',
            url     : 'management/user/update',
            data    : $.param($scope.info),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.showInfo = "修 改 成 功";
            $timeout(function(){$location.path('/userIndex/userList')},1000);
        }).error(function(data){
            $scope.showInfo = "修 改 失 败";
        });
        $timeout(function(){$scope.isHide = true;},5000);
    }
}]);

//用户添加
userApp.controller("addUser",['$scope','$http','$location','$timeout',function($scope,$http,$location,$timeout){
    $http.get('management/zone/findByStateY').success(function(data){
        $scope.zone = data;
    });
    $http.get('management/dept/findByStateY').success(function(data){
        $scope.dept = data;
    });
    $http.get('management/education/findByStateY').success(function(data){
        $scope.education = data;
    });
    $http.get('management/role/findItem').success(function(data){
        $scope.role = data;
    });
    $scope.isHideUser = true;
    $scope.userForm = {};
    $scope.userForm.job = '1';
    $scope.userForm.sex = '男';
    $scope.userForm.companyareaid = '1';
    $scope.userForm.education='1';
    $scope.userForm.jobstate='1';
    //$scope.userForm.relation='1';
    $scope.userForm.salaryState='0.8';
    //$scope.userForm.roleId='1';
    $scope.setState = function(){
        if($scope.userForm.jobstate != '1'){$scope.jsStateHide = true;$scope.userForm.salaryState='1.0'}
        else $scope.jsStateHide = false;
    };
    $scope.nextPwd = ""; function delArray(obj,id){
        var copyObj = obj;
        for(var i =0 ;i<copyObj.length;i++){
            if(copyObj[i].id == id){
                obj.splice(i,1);
            }
        }
    }
    $scope.nextPwdPass = false;
    $scope.nextPwdVail = function(){
        if($scope.nextPwd != $scope.userForm.password){
            $("#nextPwdStyle").attr('style',"border:1px solid red");
            $scope.nextPwdPass = false;
        }else{
            $("#nextPwdStyle").attr('style',"");
            $scope.nextPwdPass = true;
        }
    };
    $scope.addAccount = function(){
        if(!$scope.nextPwdPass){return;}
        $scope.userForm.entrydate = $("#entrydate").val();
        $http({
            method  : 'post',
            url     : 'management/user/add',
            data    : $.param($scope.userForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.isHideUser = false;
            if(data != 0)$scope.showErrorInfo = "系 统 检 测 到 该 用 户 已 存 在";
            else $location.path('/userIndex/userList');
        }).error(function(data){
            $scope.isHideUser = false;$scope.showErrorInfo = "服 务 器 繁 忙 稍 后 再 试";
        });
        $timeout(function(){$scope.isHideUser = true;},5000);
    }
}]);
//设置学历与区域
userApp.controller("eduOrZone",['$scope','$http','$timeout',function($scope,$http,$timeout){
    $scope.isHideEdu = true;
    $scope.isHideZone = true;
    $scope.isHideDept = true;
    $scope.eduForm = {};
    $scope.zoneForm = {};
    $scope.deptForm = {};
    $http.get('management/education/findByStateY').success(function(data){
        $scope.edu = data;
    });
    $http.get('management/zone/findByStateY').success(function(data){
        $scope.zone = data;
    });
    $http.get('management/dept/findByStateY').success(function(data){
        $scope.dept = data;
    });
    $scope.addEdu = function(){
        $http({
            method  : 'post',
            url     : 'management/education/add',
            data    : $.param($scope.eduForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.eduForm = {};
            $scope.edu.push(data);
        }).error(function(data){
            $scope.isHideEdu = false;
        });
        $timeout(function(){$scope.isHideEdu = true;},5000);;
    };
    $scope.addZone = function(){
        $http({
            method  : 'post',
            url     : 'management/zone/add',
            data    : $.param($scope.zoneForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.zoneForm = {};
            $scope.zone.push(data);
        }).error(function(data){
            $scope.isHideZone = false;
        });
        $timeout(function(){$scope.isHideZone = true;},5000);
    };
    $scope.addDept = function(){
        $http({
            method  : 'post',
            url     : 'management/dept/add',
            data    : $.param($scope.deptForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.deptForm = {};
            $scope.dept.push(data);
        }).error(function(data){
            $scope.isHideDept = false;
        });
        $timeout(function(){$scope.isHideDept = true;},5000);
    };

    $scope.delEdu = function(id){
        $http.get('management/education/del?id='+id).success(function(data){
            delArray($scope.edu,id);
        });
    };
    $scope.delZone = function(id){
        $http.get('management/zone/del?id='+id).success(function(data){
            delArray($scope.zone,id);
        });
    };
    $scope.delDept = function(id){
        $http.get('management/dept/del?id='+id).success(function(data){
            delArray($scope.dept,id);
        });
    }
}]);

//菜单添加
var  menuList;
userApp.controller("addMenu",['$scope','$http','$timeout',function($scope,$http,$timeout){
    $http.get('management/menu/findAll').success(function(data){
        $scope.menu = data;
    });
    $http.get('management/menu/findItem?yn=y').success(function(data){
        $scope.item = data;
    });
    $scope.perUpdate = false;
    $scope.isHidePer = true;
    $scope.perForm = {};
    $scope.perForm.parentId = '0';
    $scope.addPer = function(){
        $http({
            method  : 'post',
            url     : 'management/menu/add',
            data    : $.param($scope.perForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            if(!$scope.perUpdate){
                data.parentName = $("#parentMenu").find("option:selected").text();
                $scope.menu.push(data);
            }else{
                menuList.title = $scope.perForm.title;
                menuList.url = $scope.perForm.url;
                menuList.parentName = $("#parentMenu").find("option:selected").text();
                menuList.moduleName = $scope.perForm.moduleName;
                menuList.remark = $scope.perForm.remark;
                $scope.perUpdate = false;
            }
            $scope.perForm = {};
        }).error(function(data){
            $scope.isHidePer = false;
        });
        $timeout(function(){$scope.isHidePer = true;},5000);
    };
    $scope.updatePer = function(data){
        menuList = data;
        $scope.perUpdate = true;
        $scope.perForm = data;
    };
    $scope.delPer = function(id){
        $http.get('management/menu/del?id='+id).success(function(data){
            delArray($scope.menu,id);
        });
    }
}]);
//添加角色
userApp.controller("addRole",['$scope','$http','ngDialog','$timeout',function($scope,$http,ngDialog,$timeout){
    $http.get('management/per/findAll').success(function(data){
        $scope.per = data;
    });
    $scope.role = {};
    $scope.addRole = function(){
        var s=[];$('input[type="checkbox"]:checked').each(function(){s.push($(this).val());});
        $scope.role.menuId = s;
        $http({
            method  : 'post',
            url     : 'management/role/add',
            data    : $.param($scope.role),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind',showClose:false,
                remind: '设置成功', width:300, height:110
            });$scope.role = {};
        });
        $timeout(function(){ngDialog.close('management/userHandle/remind')},1000);
    };
}]);
//修改角色
userApp.filter("admin",function(){return function(input){if(input == 'admin' || input == 'phoneAdmin' || input == 'phone')return true;else return false;}});
userApp.controller("updateRole",['$scope','$http','ngDialog','$timeout','$location',function($scope,$http,ngDialog,$timeout,$location){
    $http.get('management/role/findItem').success(function(data){
        $scope.item = data;
    });
    $scope.role = {};
    $scope.id = 1;
    $scope.getRolePer = function(){
        $http.get('management/role/findInfo?id='+$scope.id).success(function(data){
            $scope.role = data.info;
            $scope.permission = data.permission;
        });
    };
    $scope.updateRole = function(){
        var s=[];$('input[type="checkbox"]:checked').each(function(){s.push($(this).val());});
        $scope.role.menuId = s;
        $scope.role.id = $scope.id;
        $http({
            method  : 'post',
            url     : 'management/role/update',
            data    : $.param($scope.role),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind',showClose:false,
                remind: '修改成功', width:300, height:110
            });
        }).error(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind', showClose:false,
                remind: '服务器繁忙稍后再试', width:350, height:110
            });
        });$timeout(function(){ngDialog.close('management/userHandle/remind')},1500);
    };
}]);
//添加权限
userApp.controller("addPermission",['$scope','$http','ngDialog','$timeout',function($scope,$http,ngDialog,$timeout){
    $http.get('management/menu/findAll').success(function(data){
        $scope.menu = data;
    });
    $scope.permission = {};
    $scope.addPer = function(){
        var s=[];$('input[type="checkbox"]:checked').each(function(){s.push($(this).val());});
        $scope.permission.menuId = s;
        $http({
            method  : 'post',
            url     : 'management/per/add',
            data    : $.param($scope.permission),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind',showClose:false,
                remind: '设置成功', width:300, height:110
            });
        }).error(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind',showClose:false,
                remind: '服务器繁忙稍后再试', width:350, height:110
            });
        });
        $timeout(function(){ngDialog.close('management/userHandle/remind')},1000);
    };
}]);
//修改权限
userApp.controller("updatePermission",['$scope','$http','ngDialog','$timeout','$location',function($scope,$http,ngDialog,$timeout,$location){
    $http.get('management/per/findItem').success(function(data){
        $scope.item = data;
    });
    $scope.permission = {};
    $scope.id = 1;
    $scope.getPerMenu = function(){
        $http.get('management/per/findInfo?id='+$scope.id).success(function(data){
            $scope.permission = data.info;
            $scope.menu = data.menu;
        });
    };
    $scope.updatePer = function(){
        var s=[];$('input[type="checkbox"]:checked').each(function(){s.push($(this).val());});
        $scope.permission.menuId = s;
        $scope.permission.id = $scope.id;
        $http({
            method  : 'post',
            url     : 'management/per/update',
            data    : $.param($scope.permission),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind',showClose:false,
                remind: '修改成功', width:300, height:110
            });
            //$timeout(function(){$location.path('/system/updatePermission');},1700);
        }).error(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind', showClose:false,
                remind: '服务器繁忙稍后再试', width:350, height:110
            });
        });
        $timeout(function(){ngDialog.close('management/userHandle/remind')},1500);
    };
}]);
